<template>
  <div style="height: 800px;overflow:hidden;background-image:url(/loginbg.png);background-size: cover;">
    <el-row :gutter="40">
      <el-col :span="13"></el-col>
      <el-col :span="11">
        <div class="login" style="width: 425px;height: 500px;margin-top: 150px;margin-left: 150px">
          <div style="width: 400px;height: 460px;margin: 0 auto ">
            <div class="form-wrap" >
              <div style="text-align: center;height: 50px;position: relative;top: 20px"><h2>手机号注册</h2></div>
              <el-form label-width="80px" label-position="top" style="color: black;" :model="user" :rules="rules" ref="formRef" >
                <el-form-item prop="phone" label="手机号">
                  <el-input placeholder="请输入手机号" v-model="user.phone"/>
                </el-form-item>
                <el-form-item prop="password" label="密码" :rules="{ required: true, message: '请输入密码', trigger: 'blur' }">
                  <el-input type="password" placeholder="请输入密码" v-model="user.password" />
                </el-form-item>
                <el-form-item prop="confirmPassword" label="确认密码" :rules="{ required: true, validator: validateRepassword, trigger: 'blur' }">
                  <el-input type="password" placeholder="请再一次输入密码" v-model="user.confirmPassword"/>
                </el-form-item>
                <el-form-item prop="code" style="position: relative;bottom: 20px">
                  <label class="form-label">验证码</label>
                  <el-row :gutter="10">
                    <el-col :span="14">
                      <el-input></el-input>
                    </el-col>
                    <el-col :span="10">
                      <el-button type="success" class="el-button-block">获取验证码</el-button></el-col>
                  </el-row>
                </el-form-item>
                <div  style="position: relative;bottom: 20px;">
                  <el-form-item label="同意协议" prop="agreeTerms">
                    <el-checkbox v-model="user.agreeTerms"></el-checkbox>
                    <span style="position: relative; bottom: 2px; font-size: 12px;">同意协议并注册《用户协议》</span>
                  </el-form-item>
                </div>
                <el-form-item>
                  <el-button type="danger" style="width: 420px;margin: 0 0 0 0" @click="reg">注册</el-button>
                </el-form-item>
              </el-form>
              <div class="ways" style="width: 70%">
                <span style=""><router-link to="/login" style="color:red;text-decoration: none">已有账号?点我登陆</router-link></span>
              </div>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import {ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";
import router from "@/router";
import qs from "qs";

const user = ref({
  phone: '',
  password: '',
  confirmPassword: '',
  agreeTerms: false
});
// 假设您有一个 ref 来引用表单
const formRef = ref(null);
// 验证同意协议的函数（如果需要的话，这里可以添加更复杂的逻辑）
const validateAgreeTerms = (rule, value, callback) => {
  if (!value) {
    callback(new Error('请同意用户协议'));
  } else {
    callback();
  }
};
const validateConfirmPassword = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'));
  } else if (value !== user.value.password) {
    callback(new Error('两次输入的密码不一致!'));
  } else {
    callback();
  }
};
const rules = {
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    // 其他可能的验证规则...
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    // 其他可能的验证规则...
  ],
  confirmPassword: [
    { required: true, validator: validateConfirmPassword, trigger: 'blur' },
  ],
  agreeTerms: [ // 新增：同意协议的验证规则
    { required: true, validator: validateAgreeTerms, trigger: 'change' },
  ]
};


const reg = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      // 确保用户已同意协议
      if (!user.value.agreeTerms) {
        ElMessage.error('请同意用户协议才能注册！');
        return;
      }
      try {
        // 使用qs库将表单数据序列化为查询字符串
        let data = qs.stringify(user.value);
        // 发送POST请求
        const response = await axios.post(BASE_URL + '/v1/users/reg', data);
        // 检查响应状态
        if (response.data.code === 2000) {
          ElMessage.success('注册成功!');
          await router.push('/login'); // 跳转到登录页面
        } else {
          ElMessage.error(response.data.msg);
        }
      } catch (error) {
        console.error('注册失败:', error);
        ElMessage.error('注册失败，请稍后再试。');
      }
    } else {
      console.log('表单验证失败，不能提交！');
      return false;
    }
  });
};




</script>


<style scoped>
.login {
  height: 100vh;
  background-color: #ffffff;

}

.form-wrap {
  width: 320px;
  margin: 0 auto;

}
.menu-tab {
  text-align: center;
  position: relative;
  li {
    display: inline-block;
    padding: 10px 24px;
    margin: 0 0;
    color: black;
    font-size: 20px;
    border-radius: 5px;
    cursor: pointer;
    &.current {
      background-color: rgba(0, 0, 0, 0.1);
    }
  }
}
.ways ul li {
  float: left;
  line-height: 18px;
  margin-left: 10px;
}
.form-label {
  display: block;
  color: #fff;
  font-size: 14px;
}
</style>